from typing import List

from fastapi import APIRouter, Request, Query
from fastapi import Depends
from requests import Session

from config.get_db import get_db
from core.entity.vo.view_models import CarModelForPage, CarMinitorObjectForQuery
from core.service.cms_car_service import CmsCarService
from module_admin.dao.dict_dao import DictDataDao
from module_admin.entity.do.dict_do import SysDictData
from utils.log_util import *
from utils.response_util import *

carMinitorController = APIRouter()


@carMinitorController.post("/car_minitor/carList", response_model=List[CarModelForPage])
async def get_car_list(request: Request, car_mo: CarMinitorObjectForQuery,
                       query_db: Session = Depends(get_db)):
    try:

        cms_car_list = CmsCarService.get_cms_car_list(query_db,car_mo)

        # Convert tuples to CarModelForPage instances
        cms_car_list_models = [CarModelForPage(**dict(zip(CarModelForPage.__fields__.keys(), car))) for car in
                               cms_car_list]

        # Encode the data to ensure it is JSON serializable
        cms_car_list_encoded: List = jsonable_encoder(cms_car_list_models)

        details: List = DictDataDao.query_dict_data_list(query_db, "yongcheleixing")
        if details:
            for e in cms_car_list_encoded:
                for detail in details:
                    if e["use_car_type"] and e["use_car_type"] == detail.dict_value:
                        e["use_car_type_name"]= detail.dict_label
        logger.info('获取成功')
        return response_200(data=cms_car_list_encoded, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
